package com.example.httpdemo.http;

import com.example.httpdemo.net.token.AuthInterceptor;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

import java.util.concurrent.TimeUnit;

public class RetrofitClient {
    private static Retrofit retrofit;
    private static OkHttpClient okHttpClient;

    private RetrofitClient() {
        // 私有构造函数，防止外部实例化
    }

    public static Retrofit getRetrofit() {
        if (retrofit == null) {
            synchronized (RetrofitClient.class) {
                if (retrofit == null) {
                    retrofit = new Retrofit.Builder()
                            .baseUrl("https://api-dev.wawo.live/") // 默认 Base URL
                            .client(getOkHttpClient())
                            .addConverterFactory(GsonConverterFactory.create())
                            .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                            .build();
                }
            }
        }
        return retrofit;
    }

    public static OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            synchronized (RetrofitClient.class) {
                if (okHttpClient == null) {
                    OkHttpClient.Builder builder = new OkHttpClient.Builder()
                            .connectTimeout(15, TimeUnit.SECONDS)
                            .readTimeout(15, TimeUnit.SECONDS)
                            .writeTimeout(15, TimeUnit.SECONDS)
                            .addInterceptor(new AuthInterceptor())
                            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)); // 日志拦截器[4](@ref)


//                    // 添加缓存（10MB）
//                    File cacheDir = new File(System.getProperty("java.io.tmpdir"), "http-cache");
//                    builder.cache(new Cache(cacheDir, 10 * 1024 * 1024));
//
//                    // 添加日志拦截器（仅在 Debug 模式下启用）
//                    if (BuildConfig.DEBUG) {
//                        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//                        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
//                        builder.addInterceptor(logging);
//                    }

                    okHttpClient = builder.build();
                }
            }
        }
        return okHttpClient;
    }

    /**
     * 动态修改 Base URL
     */
    public static void setBaseUrl(String baseUrl) {
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(getOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();
    }

    /**
     * 动态修改 OkHttp 配置
     */
    public static void setOkHttpClient(OkHttpClient client) {
        okHttpClient = client;
        retrofit = new Retrofit.Builder()
                .baseUrl(retrofit.baseUrl().toString())
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();
    }
}